package edu.mcw.rgd.clinminer.web.utils;

import java.util.Date;

import org.apache.log4j.Logger;

import edu.mcw.rgd.clinminer.domain.Measurement;
import edu.mcw.rgd.clinminer.domain.common.Recordable;
import edu.mcw.rgd.clinminer.web.userrole.ChronologicalItem;

public class MeasurementConverter extends Measurement {
	private static final Logger log = Logger.getLogger(ChronologicalItem.class);
	Measurement measurement;

	public MeasurementConverter(Recordable r) {
		this.measurement = (Measurement) r;
	}

	public String getOriginalUnit() {
		return measurement.getUnit();
	}

	public String getOriginalValue() {
		return measurement.getValue();
	}

	private Boolean isUnitCelcius() {
		if (getOriginalUnit() != null
				&& getOriginalUnit().equalsIgnoreCase("c")) {
			return true;
		} else {
			return false;
		}
	}

	private Boolean isUnitLbs() {
		if (getOriginalUnit() != null
				&& getOriginalUnit().equalsIgnoreCase("lbs")) {
			return true;
		} else {
			return false;
		}
	}

	private Boolean isUnitKul() {
		if (getOriginalUnit() != null
				&& getOriginalUnit().equalsIgnoreCase("K/ul")) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public String getUnit() {
		if (isUnitCelcius()) {
			return "F";
		} else if (isUnitLbs()) {
			return "kg";
		} else if (isUnitKul()) {
			return "/mm3";
		}
		return getOriginalUnit();
	}

	@Override
	public String getValue() {
		if (isUnitCelcius()) {
			try {
				Float celcius = new Float(getOriginalValue());
				Integer fahrenheit = (int) (celcius * (9.0 / 5.0) + 32);
				return fahrenheit.toString();
			} catch (java.lang.NumberFormatException e) {
			}
		} else if (isUnitLbs()) {
			try {
				Float lbs = new Float(getOriginalValue());
				Float kg = lbs / 2.2f;
				return String.format("%.1f", kg);
			} catch (java.lang.NumberFormatException e) {
			}
		} else if (isUnitKul()) {
			try {
				Float kul = new Float(getOriginalValue());
				Float mm3 = kul * 1000f;
				return String.format("%.0f", mm3);
			} catch (java.lang.NumberFormatException e) {
			}
		}
		return getOriginalValue();
	}

	@Override
	public Long getId() {
		return measurement.getId();
	}

	@Override
	public String getLOINCID() {
		return measurement.getLOINCID();
	}

	@Override
	public String getLOINCLabel() {
		return measurement.getLOINCLabel();
	}

	@Override
	public String getAnnotatedText() {
		return measurement.getAnnotatedText();
	}

	@Override
	public String getDocumentSource() {
		return measurement.getDocumentSource();
	}

	@Override
	public Date getDocumentSourceDate() {
		return measurement.getDocumentSourceDate();
	}

	@Override
	public Date getEventDate() {
		return measurement.getEventDate();
	}

	@Override
	public Boolean getExactDate() {
		return measurement.getExactDate();
	}

	@Override
	public Boolean getDirectObservation() {
		return measurement.getDirectObservation();
	}

	@Override
	public String getOntologyTermID() {
		return measurement.getOntologyTermID();
	}

	@Override
	public String getOntologyTermLabel() {
		return measurement.getOntologyTermLabel();
	}

	@Override
	public String getNotes() {
		return measurement.getNotes();
	}

	@Override
	public Date getSingleDate() {
		return measurement.getSingleDate();
	}

	@Override
	public Integer getEvidenceScore() {
		return measurement.getEvidenceScore();
	}

}
